<?php

namespace app;


class Requests
{

	/**
	 * 获取请求的数据
	 * @param array $params
	 * @param bool $suffix
	 * @param bool $filter
	 * @return array
	 */
	public function more(array $params, bool $suffix = false, bool $filter = true): array
	{
		$p = [];
		$i = 0;
		foreach ($params as $param) {
			if (!is_array($param)) {
				$p[$suffix == true ? $i++ : $param] = $this->param($param);
			} else {
				if (!isset($param[1])) $param[1] = null;
				if (!isset($param[2])) $param[2] = '';
				if (is_array($param[0])) {
					$name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
					$keyName = $param[0][0];
				} else {
					$name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
					$keyName = $param[0];
				}

				$p[$suffix == true ? $i++ : ($param[3] ?? $keyName)] = $this->param($name, $param[1], $param[2]);
			}
		}

		if ($filter && $p) {
			$p = $this->filterArrayValues($p);
		}

		return $p;
	}

	/**
	 * @param $array
	 * @return array
	 */
	public function filterArrayValues($array)
	{
		$result = [];
		foreach ($array as $key => $value) {
			if (is_array($value)) {
				// 如果值是数组，递归调用 filterArrayValues
				$result[$key] = in_array($key, $this->except) ? $value : $this->filterArrayValues($value);
			} else {
				if (in_array($key, $this->except) || is_int($value) || is_null($value)) {
					$result[$key] = $value;
				} else {
					// 如果值是字符串，过滤特殊字符
					$result[$key] = filter_str($value);
				}

			}
		}
		return $result;
	}


    /**
     * 获取get参数
     * @param array $params
     * @param bool $suffix
     * @param bool $filter
     * @return array
     */
    public function getMore(array $params, bool $suffix = false, bool $filter = true): array
    {
        return $this->more($params, $suffix, $filter);
    }

    /**
     * 获取post参数
     * @param array $params
     * @param bool $suffix
     * @param bool $filter
     * @return array
     */
    public function postMore(array $params, bool $suffix = false, bool $filter = true): array
    {
        return $this->more($params, $suffix, $filter);
    }


    /**
     * 获取ip
     * @return string
     */
    public function ip(): string
    {
        if ($this->server('HTTP_CLIENT_IP', '')) {
            $ip = $this->server('HTTP_CLIENT_IP', '');
        } elseif ($this->server('HTTP_X_REAL_IP', '')) {
            $ip = $this->server('HTTP_X_REAL_IP', '');
        } elseif ($this->server('HTTP_X_FORWARDED_FOR', '')) {
            $ip = $this->server('HTTP_X_FORWARDED_FOR', '');
            $ips = explode(',', $ip);
            $ip = $ips[0];
        } elseif ($this->server('REMOTE_ADDR', '')) {
            $ip = $this->server('REMOTE_ADDR', '');
        } else {
            $ip = '0.0.0.0';
        }
        return $ip;
    }



}
